package com.yay.easter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater.Factory;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 * 
 * 
 * Examples: FingerPaint, Compressed Texture, Patterns, RoundRect,
 *  Reorder Activities, Notification, SMS, Text Linkify, Views Animation Shake, Randomise/Wave Scale,
 *  TableLayout/Toggle Shrink
 *  
 *  http://games.vnutri.info/kids-game/krasivye-pashalnye-yayca-4384/
 *  Done: SurfaceView Overlay, Gallery/ImageSwitcher, TranslucentGLSurfaceView
 */
public class Egg extends Activity implements OnClickListener {
	private SharedPreferences prefs;
	private final String LANG_KEY = "com.yay.easter.lang";
    private TouchSurfaceView mGLSurfaceView;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L10N.locale = new Locale(getPreferences().getString(LANG_KEY, "ru"));
        L10N.init(this.getResources());
        setContentView(R.layout.main);
        
        // Egg
        FrameLayout container = (FrameLayout) findViewById(R.id.container);
        mGLSurfaceView = new TouchSurfaceView(this);
        
        container.addView(mGLSurfaceView);
        mGLSurfaceView.requestFocus();
        mGLSurfaceView.setFocusableInTouchMode(true);
        
        //mGLSurfaceView.getRenderer().setTexture(mImageIds[0]);
        
        // Gallery
        // Reference the Gallery view
        Gallery g = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        g.setAdapter(new ImageAdapter(this));
        
        // Set a item click listener, and just Toast the clicked position
        g.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView parent, View v, int position, long id) {
        		mGLSurfaceView.getRenderer().setTexture(mImageIds[position]);
            }
        });
        
        // We also want to show context menu for longpressed items in the gallery
        registerForContextMenu(g);
        
    }
	
	@Override
	public void onClick(View arg0) {
    
	}
	
    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
    }


	protected void setMenuBackground() {
		  getLayoutInflater().setFactory(new Factory() {
			  @Override
			  public View onCreateView(String name, Context context, AttributeSet attrs) {
			      if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
			        try {
			          LayoutInflater f = getLayoutInflater();
			          final View view = f.createView(name, null, attrs);
			          new Handler().post(new Runnable() {
			            public void run() {
			            	TextView text = (TextView) view; 
	                        text.setBackgroundResource(R.drawable.button_shape);
	                        text.setTextAppearance(Egg.this, R.style.contextItem); 
	                        text.setTypeface(L10N.font);
			            }
			          });
			          return view;
			        } catch (Exception e) {
			        }
			      }
			      return null;
			  }
		  });
		}
		
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
		    MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.menu.language, menu);
	        setMenuBackground();
		    return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
		    switch (item.getItemId()) {
			    case R.id.ru:
			    	changeLocale("ru");
			    	return true;
			    case R.id.ua:
			    	changeLocale("ua");
					return true;
			    case R.id.en:
			    	changeLocale("en");
					return true;
			    default: finish();
			    	return true;
		    }
		}
		
		@Override
	    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	        menu.add(R.string.app_name);
	    }
	    
	    @Override
	    public boolean onContextItemSelected(MenuItem item) {
	        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
	        return true;
	    }

	    public class ImageAdapter extends BaseAdapter {
	        int mGalleryItemBackground;
	        
	        public ImageAdapter(Context c) {
	            mContext = c;
	            // See res/values/attrs.xml for the <declare-styleable> that defines Gallery1.
	            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
	            mGalleryItemBackground = a.getResourceId(
	                    R.styleable.Gallery1_android_galleryItemBackground, 0);
	            a.recycle();
	        }

	        public int getCount() {
	            return mImageIds.length;
	        }

	        public Object getItem(int position) {
	            return position;
	        }

	        public long getItemId(int position) {
	            return position;
	        }

	        public View getView(int position, View convertView, ViewGroup parent) {
	            ImageView i = new ImageView(mContext);

	            i.setImageResource(mImageIds[position]);
	            i.setScaleType(ImageView.ScaleType.FIT_XY);
	            //i.setLayoutParams(new Gallery.LayoutParams(136, 88));
	            i.setLayoutParams(new Gallery.LayoutParams(150, LayoutParams.FILL_PARENT));
	            
	            // The preferred Gallery item background
	            i.setBackgroundResource(mGalleryItemBackground);
	            
	            return i;
	        }

	        private Context mContext;


	    }
	    
        private Integer[] mImageIds = {
                R.drawable.t1,
                R.drawable.t2,
                R.drawable.t3,
                R.drawable.t4,
                R.drawable.t5,
                R.drawable.t6,
                R.drawable.t7
        };
        
	    // PRIVATE METHODS
		
		private void changeLocale(String locale) {
			L10N.locale = new Locale(locale);
	    	Editor edit = getPreferences().edit();
			edit.putString(LANG_KEY, locale);
			edit.commit();
			L10N.init(this.getResources());
		}
	    
		private SharedPreferences getPreferences() {
			if(prefs == null) {
				prefs = PreferenceManager.getDefaultSharedPreferences(this);
			}
			return prefs;
		}
	
}